//
//  ZXY_DownLoadUtility.m
//  DownLoadTest
//
//  Created by 宇周 on 15/8/25.
//  Copyright (c) 2015年 宇周. All rights reserved.
//

#import "ZXY_DownLoadUtility.h"

@implementation ZXY_DownLoadUtility
static NSString *fileName = @"ZXYDownLoadActionItem.plist";
static NSString *fileSuperDire = @"ZXYDownLoadDirectory";
+ (void)writeActionItemToFile:(ZXY_DownItem *)itemToWrite
{
    NSString *downKey      = itemToWrite.downKey;
    NSString *downPath     = itemToWrite.downPath;
    NSString *downSavePath = itemToWrite.downSavePath;
    NSString *status       = itemToWrite.status;
    NSData   *resumeData   = itemToWrite.resumeData;
    NSString *filePath = [self createFileAtPath];
    
    NSArray  *itemArr  = [NSArray arrayWithContentsOfFile:filePath];
    NSMutableArray  *arrayToWrite = [[NSMutableArray alloc] init];
    if([self itemFromFileWithKey:downKey])
    {
        return;
    }
    NSDictionary *itemDic = @{@"downKey" : downKey , @"downPath" : downPath , @"downSavePath" : downSavePath , @"status" : status , @"resumeData": resumeData};
    
    if(itemArr == nil)
    {
        [arrayToWrite addObject:itemDic];
    }
    else
    {
        [arrayToWrite addObjectsFromArray:itemArr];
        [arrayToWrite addObject:itemDic];
    }
    [arrayToWrite writeToFile:filePath atomically:YES];
    
}

+ (void)removeActionItemFromFile:(ZXY_DownItem *)itemToRemove
{
    NSString *filePath = [self createFileAtPath];
    NSArray  *itemArr  = [NSArray arrayWithContentsOfFile:filePath];
    if(itemArr == nil)
    {
        return;
    }
    NSMutableArray *removeArr = [NSMutableArray arrayWithArray:itemArr];
    NSDictionary *removeItem;
    for(int i = 0 ; i < removeArr.count ; i++)
    {
        NSDictionary *current = removeArr[i];
        if([current[@"downKey"] isEqualToString:itemToRemove.downKey])
        {
            removeItem = current;
        }
    }
    if(removeItem)
    {
        [removeArr removeObject:removeItem];
    }
    [removeArr writeToFile:filePath atomically:YES];
}

+ (void)removeActionItemFromFileWithKey:(NSString *)itemKey
{
    NSString *filePath = [self createFileAtPath];
    NSArray  *itemArr  = [NSArray arrayWithContentsOfFile:filePath];
    if(itemArr == nil)
    {
        return;
    }
    NSMutableArray *removeArr = [NSMutableArray arrayWithArray:itemArr];
    NSDictionary *removeItem;
    for(int i = 0 ; i < removeArr.count ; i++)
    {
        NSDictionary *current = removeArr[i];
        if([current[@"downKey"] isEqualToString:itemKey])
        {
            removeItem = current;
        }
    }
    if(removeItem)
    {
        [removeArr removeObject:removeItem];
    }
    [removeArr writeToFile:filePath atomically:YES];
}

+ (ZXY_DownItem   *) itemFromFileWithKey:(NSString *)key
{
    NSString *filePath = [self createFileAtPath];
    NSArray  *itemArr  = [NSArray arrayWithContentsOfFile:filePath];
    for(NSDictionary *currentDic in itemArr)
    {
        ZXY_DownItem *currentItem = [self dicToItem:currentDic];
        if([currentItem.downKey isEqualToString:key])
        {
            return currentItem;
        }
    }
    return nil;
}

+ (NSMutableArray *) itemArrFromFile
{
    NSString *filePath = [self createFileAtPath];
    NSMutableArray *itemArrForReturn = [[NSMutableArray alloc] init];
    NSArray  *itemArr  = [NSArray arrayWithContentsOfFile:filePath];
    for(NSDictionary *currentDic in itemArr)
    {
        ZXY_DownItem *item = [self dicToItem:currentDic];
        [itemArrForReturn addObject:item];
    }
    return itemArrForReturn;
}

+ (NSString *)createFileAtPath
{
    NSString *documentPath = [[ZXYFileOperateHelper sharedInstance] documentPath];
    NSString *downDirec    = [documentPath stringByAppendingPathComponent:fileSuperDire];
    NSString *filePath     = [downDirec stringByAppendingPathComponent:fileName];
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        return filePath;
    }
    else
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:downDirec withIntermediateDirectories:YES attributes:nil error:nil];
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
    }
    return filePath;
}

+ (ZXY_DownItem *)dicToItem:(NSDictionary *)dic
{
    if(dic == nil)
    {
        return nil;
    }
    NSString *downKey      = dic[@"downKey"];
    NSString *downPath     = dic[@"downPath"];
    NSString *downSavePath = dic[@"downSavePath"];
    NSData   *resumeData   = dic[@"resumeData"];
    NSString *status       = dic[@"status"];
    ZXY_DownItem *item = [[ZXY_DownItem alloc] init];
    item.downKey = downKey;
    item.downPath = downPath;
    item.downSavePath = downSavePath;
    item.status = status;
    item.resumeData = resumeData;
    return item;
}

+ (NSDictionary *)itemToDic:(ZXY_DownItem *)item
{
    NSString *downKey      = item.downKey;
    NSString *downPath     = item.downPath;
    NSString *downSavePath = item.downSavePath;
    NSData   *resumeData   = item.resumeData;
    NSString *status       = item.status;
    NSDictionary *itemDic = @{@"downKey" : downKey , @"downPath" : downPath , @"downSavePath" : downSavePath , @"status" : status , @"resumeData" : resumeData};
    return itemDic;
}

+ (void)setResumeDataForKey:(NSString *)key andData:(NSData *)resumeData
{
    if(!resumeData)
    {
        return;
    }
    NSString *filePath = [self createFileAtPath];
    NSArray  *itemArr  = [NSArray arrayWithContentsOfFile:filePath];
    NSMutableArray *saveArr = [[NSMutableArray alloc] init];
    if(itemArr == nil)
    {
        return;
    }
    for(NSDictionary *currentDic in itemArr)
    {
        
        ZXY_DownItem *item = [ZXY_DownLoadUtility itemFromFileWithKey:key];
        NSString *downKey = item.downKey;
        if([downKey isEqualToString:key])
        {
            NSMutableData *dataMulty = [NSMutableData dataWithData:item.resumeData];
            [dataMulty appendData:resumeData];
            item.resumeData = dataMulty;
            NSDictionary *neDic = [ZXY_DownLoadUtility itemToDic:item];
            [saveArr addObject:neDic];
        }
        else
        {
            [saveArr addObject:currentDic];
        }
    }
    [saveArr writeToFile:filePath atomically:YES];
}

+ (void)setStatusForKey:(NSString *)key andStatus:(NSString *)status
{
    if(!status)
    {
        return;
    }
    NSString *filePath = [self createFileAtPath];
    NSArray  *itemArr  = [NSArray arrayWithContentsOfFile:filePath];
    NSMutableArray *saveArr = [[NSMutableArray alloc] init];
    if(itemArr == nil)
    {
        return;
    }
    for(NSDictionary *currentDic in itemArr)
    {
        
        ZXY_DownItem *item = [ZXY_DownLoadUtility itemFromFileWithKey:key];
        NSString *downKey = item.downKey;
        if([downKey isEqualToString:key])
        {
            item.status = status;
            NSDictionary *neDic = [ZXY_DownLoadUtility itemToDic:item];
            [saveArr addObject:neDic];
        }
        else
        {
            [saveArr addObject:currentDic];
        }
    }
    [saveArr writeToFile:filePath atomically:YES];
}

@end
